package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.service.ICustomerService;
import com.briup.service.impl.CustomerServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 登录
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //实例化servlet层对象
        ICustomerService customerService = new CustomerServiceImpl();
        //接收login传入的参数
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        HttpSession session = req.getSession();
        try {
            Customer customer = customerService.login(name, password);
            //如果成功的话，跳转到index.jsp主页
            session.setAttribute("customer", customer);

            resp.sendRedirect("index.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            String message = e.getMessage();
            session.setAttribute("error", message);
            //如果有异常，跳转到login.jsp登录页
            resp.sendRedirect("login.jsp");
        }
    }

}
